//
//  SCNInvestDetailCell1.swift
//  ShoucainuSwift
//
//  Created by 王家旭 on 2017/11/21.
//  Copyright © 2017年 Riluee. All rights reserved.
//

import UIKit

class SCNInvestDetailCell1: UICollectionViewCell {
    
    lazy var imgView: UIImageView = {
        let view = UIImageView(image: UIImage(named: "项目详情背景"))
        return view
    }()
    lazy var retaL: UILabel = {
        let lab = UILabel()
        lab.textColor = .white
//        lab.text = "8.80%"
        lab.font = UIFont.systemFont(ofSize: 35.0)
        return lab
    }()
    lazy var hikeOutL: UILabel = {
        let lab = UILabel()
        lab.text = "+4.20%"
        lab.font = UIFont.systemFont(ofSize: 15.0)
        lab.textColor = .white
        lab.textAlignment = .center
        lab.backgroundColor = SCNConstants.colorWithHexString("ff8d1a")
        lab.layer.cornerRadius = 5.0
        lab.layer.masksToBounds = true
        lab.isHidden = true
        return lab
    }()
    lazy var retaTextL: UILabel = {
        let lab = UILabel()
        lab.textColor = SCNConstants.colorWithHexString("9ed8ff")
        lab.text = "年利率"
        lab.font = UIFont.systemFont(ofSize: 20.0)
        return lab
    }()
    lazy var repayL: UILabel = {
        let lab = UILabel()
        lab.text = "本息到付"
        lab.font = UIFont.systemFont(ofSize: 17.0)
        lab.textColor = .white
        lab.textAlignment = .center
        lab.layer.cornerRadius = 5.0
        lab.layer.masksToBounds = true
        lab.layer.borderWidth = 0.5
        lab.layer.borderColor = UIColor.white.cgColor
        return lab
    }()
    lazy var timeTextL: UILabel = {
        let lab = UILabel()
        lab.textColor = SCNConstants.colorWithHexString("9ed8ff")
        lab.text = "期限"
        lab.font = UIFont.systemFont(ofSize: 15.5)
        lab.textAlignment = .center
        return lab
    }()
    lazy var priceTextL: UILabel = {
        let lab = UILabel()
        lab.textColor = SCNConstants.colorWithHexString("9ed8ff")
        lab.text = "金额"
        lab.font = UIFont.systemFont(ofSize: 15.5)
        lab.textAlignment = .center
        return lab
    }()
    lazy var canTextL: UILabel = {
        let lab = UILabel()
        lab.textColor = SCNConstants.colorWithHexString("9ed8ff")
        lab.text = "可投金额"
        lab.font = UIFont.systemFont(ofSize: 15.5)
        lab.textAlignment = .center
        return lab
    }()
    lazy var timeL: UILabel = {
        let lab = UILabel()
        lab.textColor = .white
        lab.text = "10天"
        lab.font = UIFont.systemFont(ofSize: 17.0)
        lab.textAlignment = .center
        return lab
    }()
    lazy var priceL: UILabel = {
        let lab = UILabel()
        lab.textColor = .white
        lab.text = "2万"
        lab.font = UIFont.systemFont(ofSize: 17.0)
        lab.textAlignment = .center
        return lab
    }()
    lazy var canL: UILabel = {
        let lab = UILabel()
        lab.textColor = .white
        lab.text = "6千"
        lab.font = UIFont.systemFont(ofSize: 17.0)
        lab.textAlignment = .center
        return lab
    }()
    lazy var videoBtn: UIButton = {
        let btn = UIButton()
        btn.backgroundColor = .clear
        btn.isEnabled = false
        return btn
    }()
    lazy var progressView: SCNCircleLoadView = {
        
        let view = SCNCircleLoadView()
        view.circleRadius = 22
        view.progressTextL.textColor = .white
        view.circlePathLayer.strokeColor = SCNConstants.colorWithHexString("54edfb").cgColor
        return view
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        configurationUI()
    }
    // MARK: UI
    func configurationUI() {
        
        self.contentView.addSubview(imgView)
        imgView.addSubview(retaL)
        imgView.addSubview(hikeOutL)
        imgView.addSubview(retaTextL)
        imgView.addSubview(repayL)
        imgView.addSubview(timeTextL)
        imgView.addSubview(priceTextL)
        imgView.addSubview(canTextL)
        imgView.addSubview(timeL)
        imgView.addSubview(priceL)
        imgView.addSubview(canL)
        imgView.addSubview(progressView)
        self.contentView.addSubview(videoBtn)
        
        let imgH = SCNConstants.SCREENWIDTH/360*320
        let line1W = SCNConstants.SCREENWIDTH/4
        // 竖线
        for index in 1..<4 {
            let line1 = UIView(frame: CGRect(x: CGFloat(index)*line1W, y: imgH-55, width: 1, height: 45))
            line1.backgroundColor = SCNConstants.colorWithHexString("cee7ff")
            imgView.addSubview(line1)
        }
        //过渡线
        let transitView1 = UIView()
        transitView1.backgroundColor = SCNConstants.colorWithHexString("cde7ff")
        self.contentView.addSubview(transitView1)
        
        let transitView2 = UIView()
        transitView2.backgroundColor = SCNConstants.colorWithHexString("f1f1f1")
        self.contentView.addSubview(transitView2)
        
        
        
        imgView.snp.makeConstraints { (make) in
            make.leading.top.trailing.equalToSuperview()
            make.height.equalTo(imgH)
        }
        retaL.snp.makeConstraints { (make) in
            make.top.equalTo(20)
            make.centerX.equalToSuperview()
        }
        retaTextL.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(retaL.snp.bottom).offset(5)
        }
        repayL.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(retaTextL.snp.bottom).offset(30)
            make.size.equalTo(CGSize(width: 80, height: 20))
        }
        timeTextL.snp.makeConstraints { (make) in
            make.leading.equalToSuperview()
            make.top.equalTo(imgH - 55)
            make.size.equalTo(CGSize(width: line1W, height: 20))
        }
        priceTextL.snp.makeConstraints { (make) in
            make.leading.equalTo(timeTextL.snp.trailing)
            make.top.equalTo(imgH - 55)
            make.size.equalTo(CGSize(width: line1W, height: 20))
        }
        canTextL.snp.makeConstraints { (make) in
            make.leading.equalTo(priceTextL.snp.trailing)
            make.top.equalTo(imgH - 55)
            make.size.equalTo(CGSize(width: line1W, height: 20))
        }
        timeL.snp.makeConstraints { (make) in
            make.size.leading.equalTo(timeTextL)
            make.top.equalTo(timeTextL.snp.bottom).offset(5)
        }
        priceL.snp.makeConstraints { (make) in
            make.size.leading.equalTo(priceTextL)
            make.top.equalTo(priceTextL.snp.bottom).offset(5)
        }
        canL.snp.makeConstraints { (make) in
            make.size.leading.equalTo(canTextL)
            make.top.equalTo(canTextL.snp.bottom).offset(5)
        }
        hikeOutL.snp.makeConstraints { (make) in
            make.leading.equalTo(retaL.snp.trailing).offset(-20)
            make.top.equalTo(retaL.snp.top).offset(10)
            make.size.equalTo(CGSize(width: 65, height: 21))
        }
        progressView.snp.makeConstraints { (make) in
            make.size.equalTo(CGSize(width: 44, height: 44))
            make.top.equalTo(imgH - 54)
            make.leading.equalTo(imgView.snp.trailing).offset(-(line1W/2+22))
        }
        transitView1.snp.makeConstraints { (make) in
            make.leading.trailing.equalToSuperview()
            make.height.equalTo(10)
            make.top.equalTo(imgView.snp.bottom)
        }
        videoBtn.snp.makeConstraints { (make) in
            make.leading.trailing.equalToSuperview()
            make.height.equalTo(80)
            make.top.equalTo(transitView1.snp.bottom)
        }
        transitView2.snp.makeConstraints { (make) in
            make.leading.trailing.equalToSuperview()
            make.height.equalTo(10)
            make.top.equalTo(videoBtn.snp.bottom)
        }
        
        let line = UILabel()
        line.backgroundColor = SCNConstants.colorWithHexString("dddddd")
        self.contentView.addSubview(line)
        line.snp.makeConstraints { (make) in
            make.leading.equalTo(20)
            make.trailing.equalTo(-20)
            make.height.equalTo(0.5)
            make.bottom.equalTo(-40)
        }
        let hintLab = UILabel()
        hintLab.text = "上拉滑动，查看更多详情"
        hintLab.textColor = SCNConstants.colorWithHexString("c9c9c9")
        hintLab.font = UIFont.systemFont(ofSize: 14.5)
        hintLab.backgroundColor = .white
        self.contentView.addSubview(hintLab)
        hintLab.sizeToFit()
        hintLab.snp.makeConstraints { (make) in
            make.center.equalTo(line.snp.center)
        }
        let arrowsImgView = UIImageView(image: UIImage(named: "icon-shangla"))
        self.contentView.addSubview(arrowsImgView)
        arrowsImgView.snp.makeConstraints { (make) in
            make.size.equalTo(CGSize(width: 12,height: 12))
            make.centerX.equalTo(line.snp.centerX)
            make.centerY.equalTo(line.snp.centerY).offset(-25)
        }
    }
    
    
    func setSCNInvestDetailCell1(_ model: SCNInvestDetailDataresultModel,index: IndexPath) {
        //年利率
        retaL.text = "\(model.originrate!)%"
        retaL.attributedText = String().changeAttributedString(retaL.text!, range: NSRange(location: 0, length: (retaL.text?.characters.count)! - 1), font: 70, color: UIColor.white)
        //加息
        if let extrarate = model.extrarate {
            if extrarate == "0.00" || extrarate.characters.count == 0 {
                hikeOutL.isHidden = true
            }else {
                hikeOutL.isHidden = false
                hikeOutL.text = "+\(extrarate)%"
            }
        }
        //返息方式
        repayL.text = model.repay
        //期限
        timeL.text = model.loandue
        //金额
        priceL.text = model.loanmoneyFormat
        //可投金额
        canL.text = model.maxtendermoney
        //广告
        videoBtn.kf.setImage(with: URL(string: model.activity!["content"] as! String), for: .normal, placeholder: nil, options: nil, progressBlock: nil) { (img, err, cache, url) in
            self.videoBtn.isEnabled = true
        }
        //进度
        progressView.progress = CGFloat(model.progress!)
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
